package simtester.server



import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Instance)
class InstanceTests {
    def testuser
    def testcase
    def testrun
    
    @Before
    void setUp() {
        testuser = new User(username: 'testuser', password: 'pass')
        testcase = new TestCase(name: 'MyTestCase', script: 'my script', owner: testuser)
        testrun  = new TestRun(status: 'running', testcase: testcase)
    }

    @Test
    void createInstanceSuccessfully() {
        def instance = new Instance(testrun: testrun,
                                    cloudId: 'i-39555b7e',
                                    amiId:   'ami-1bd68a5e',
                                    type:    'm1.large',
                                    status:  'free')
        assert instance.validate()
    }
    
    @Test
    void createInstanceWithoutRequiredFields() {
        def inst = new Instance()
        assert !inst.validate()
        assert inst.errors.errorCount == 4
        assert 'nullable' in inst.errors['cloudId'].codes
        assert 'nullable' in inst.errors['amiId'].codes
        assert 'nullable' in inst.errors['type'].codes
        assert 'nullable' in inst.errors['status'].codes
    }
}
